package com.zlx.mall.basic.common.exception;


import java.util.concurrent.Callable;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author qingke
 * @see org.springframework.util.ReflectionUtils
 **/
public abstract class ExceptionWrapper {

    public static <T, E extends BizRuntimeException>
    T wrap(Callable<T> callable, Function<Throwable, ? extends E> wrapper) {
        try {
            return callable.call();
        } catch (Throwable e) {
            throw wrapper.apply(e);
        }
    }

    public static <R extends BizRuntimeException>
    void rethrowException(Throwable e, Supplier<? extends R> wrapper) {
        if (e instanceof BizRuntimeException) {
            throw ((BizRuntimeException) e);
        }
        throw wrapper.get();
    }
}
